//
//  ZLAlert.swift
//  ZLAlert
//
//  Created by ZL on 2022/11/24.
//

import UIKit
import SnapKit
/// 获取刚添加的视图
public typealias ZLAlertCustom<T> = (_ lastView: T) -> Void
/// 弹窗
@objcMembers public class ZLAlert: UIViewController {
    /// 主视图
    lazy var mainView: ZLAlertMainView = {
        let view = ZLAlertMainView()
        view.backgroundColor = UIColor.white
        view.layer.masksToBounds = true
        view.layer.cornerRadius = 5
        return view
    }()
    
    public var mainBackgroundColor: UIColor = UIColor.white {
        didSet {
            self.mainView.backgroundColor = mainBackgroundColor
        }
    }
    
    var tapClose: Bool = ZLAlertConfig.shared.tapBackgroundClose
    
    public convenience init(tapClose: Bool = ZLAlertConfig.shared.tapBackgroundClose) {
        self.init()
        self.tapClose = tapClose
    }
    
    init() {
        super.init(nibName: nil, bundle: nil)
        self.modalPresentationStyle = .overFullScreen
        self.transitioningDelegate = self
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(self.mainView)
        self.mainView.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(35)
            make.right.equalTo(self.view).offset(-35)
            make.centerY.equalTo(self.view)
        }
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide(_:)), name: UIResponder.keyboardDidHideNotification, object: nil)
    }

    public override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }
        let location = touch.location(in: self.view)
        if self.mainView.frame.contains(location) {
            return
        }
        if self.tapClose {
            self.dismiss(animated: true)
            return
        }
        super.touchesBegan(touches, with: event)
    }
    /// 自定义最后一个view，没有则是主view
    /// - Parameter call: 回调
    /// - Returns: ZLAlert
    @discardableResult
    public func custom<T>(_ call: ZLAlertCustom<T>) -> ZLAlert {
        let lastView = self.mainView.subviews.last
        if (lastView == nil) {
            call(self.mainView as! T)
        } else {
            call(lastView! as! T)
        }
        return self
    }
    
    /// 显示
    public func show() {
        let topVC = UIViewController.zl_topViewController()
        topVC?.present(self, animated: true)
    }
    /// 设置高度
    func layoutMainView(_ bottom: CGFloat) -> ZLAlert {
        let lastView = self.mainView.subviews.last
        if (lastView == nil) {
            return self
        }
        self.mainView.snp.remakeConstraints({ make in
            make.bottom.equalTo(lastView!.snp.bottom).offset(bottom)
        })
        return self
    }
}

extension ZLAlert: UIViewControllerTransitioningDelegate {
    public func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return ZLAlertAnimation(isPresenting: true)
    }
    
    public func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return ZLAlertAnimation(isPresenting: false)
    }
}

